package com.ea.report.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ea.common.report.pojo.vo.*;

import com.ea.common.utils.AgeUtils;
import com.ea.report.mapper.*;
import com.ea.report.pojo.dto.DashboardDepartmentWarningDto;
import com.ea.report.pojo.dto.WarningPersonDto;
import com.ea.report.service.DashboardService;
import com.ea.report.service.StatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


import java.util.*;


import java.util.stream.Collectors;

@Service
public class DashboardServiceImpl implements DashboardService {
    @Resource
    private ReportInfoMapper reportInfoMapper;
    @Resource
    ReportPropertiesMapper reportPropertiesMapper;
    @Resource
     StatsGroupPersonalityMapMapper statsGroupPersonalityMapMapper;

    @Resource
    StatsGroupEmotionalMapMapper statsGroupEmotionalMapMapper;

    @Resource
    StatsWarningPropertiesMapper statsWarningPropertiesMapper;

    @Resource
    StatsService statsService;

    @Override
    public DashboardVo getDashboardVo(List<Long> companyIdList, List<Long> deptIdList,
                                      Integer totalCount,
                                      Integer totalCountFemale,
                                      Integer totalCountMale,String startDate, String endDate) {
        DashboardVo dashboardVo=new DashboardVo();

        // 获取总统计数据
        dashboardVo = parseCountDashboardVo(companyIdList, deptIdList, startDate, endDate,totalCount,totalCountFemale,totalCountMale);

        //获取预警人员分布图
        List<DashboardDepartmentWarningDto> records=reportInfoMapper.getDashboardDepartmentWarningVo(companyIdList, deptIdList, startDate, endDate);
        dashboardVo.setDashboardDepartmentWarningVoList(parseDepartmentWarningMap(records));

        //人员情绪分布
        StatsGroupEmotionalMapVo statsGroupEmotionalMapVo = statsService.getStatsGroupEmotionalMapVo(startDate,endDate,companyIdList,deptIdList);
        dashboardVo.setStatsGroupEmotionalMapVo(statsGroupEmotionalMapVo);

        //人员性格分布（分男女）
        DashboardPersonalityVo dashboardPersonalityVo=statsGroupPersonalityMapMapper.selectGroupPersonalityMapDVo(startDate, endDate,companyIdList, deptIdList);
        dashboardVo.setDashboardPersonalityVo(dashboardPersonalityVo);

        //预警名单
        List<Integer> warningTypeList = new ArrayList<>();//三种预警
        warningTypeList.add(1);
        warningTypeList.add(2);
        warningTypeList.add(3);
        List<WarningPersonDto> WarningPersonDtoRecords = reportInfoMapper.getWarningPersonDtoOfDash(companyIdList, deptIdList, null, null, warningTypeList, startDate, endDate, new Page<>(1, totalCount)).getRecords();
        if (!WarningPersonDtoRecords.isEmpty()) {//预警人员名单
            dashboardVo.setAllWarningPersonList(parseWarningPersonDtoListToVo(WarningPersonDtoRecords));
        }


        //预警分类图
        List<DashboardWarningPropertiesVo> dashboardWarningPropertiesVoList=statsWarningPropertiesMapper.selectGroupWarningPropertiesDVo(startDate,endDate,companyIdList,deptIdList);
        if(dashboardWarningPropertiesVoList!=null)
        {
            if(dashboardWarningPropertiesVoList.isEmpty()||
                 dashboardWarningPropertiesVoList.size()!=12){
            dashboardWarningPropertiesVoList=statsWarningPropertiesMapper.selectGroupWarningPropertiesDVoEmpty(null,null,null,null);
        }
            // 定义排序顺序
            int[] indexes = {10, 11, 3, 4, 2, 6, 8, 9, 0, 1, 5, 7};

            // 创建一个新的 List 并按照指定顺序排序
            List<DashboardWarningPropertiesVo> sortedList = new ArrayList<>(dashboardWarningPropertiesVoList.size());
            for (int index : indexes) {
                sortedList.add(dashboardWarningPropertiesVoList.get(index));
            }
            dashboardVo.setDashboardWarningPropertiesVos(sortedList);
        }else{
            dashboardVo.setDashboardWarningPropertiesVos(null);
        }


        return dashboardVo;
    }

    private DashboardVo parseCountDashboardVo (List<Long> companyIdList, List<Long> deptIdList,String startDate, String endDate,
                                               Integer totalCount,Integer totalCountFemale,
                                               Integer totalCountMale){
        DashboardVo dashboardVo=new DashboardVo();

        // 获取总统计数据
        DashboardVo stats = reportInfoMapper.getDashboardStats(companyIdList, deptIdList, startDate, endDate);

        dashboardVo.setTotalCount(totalCount);
        dashboardVo.setTotalCountMale(totalCountMale);
        dashboardVo.setTotalCountFemale(totalCountFemale);

        dashboardVo.setResponseWarningCount(stats.getResponseWarningCount());
        dashboardVo.setResponseWarningCountMale(stats.getResponseWarningCountMale());
        dashboardVo.setResponseWarningCountFemale(stats.getResponseWarningCountFemale());



        dashboardVo.setAdaptiveWarningCount(stats.getAdaptiveWarningCount());
        dashboardVo.setAdaptiveWarningCountMale(stats.getAdaptiveWarningCountMale());
        dashboardVo.setAdaptiveWarningCountFemale(stats.getAdaptiveWarningCountFemale());

        dashboardVo.setRiskWarningCount(stats.getRiskWarningCount());
        dashboardVo.setRiskWarningCountMale(stats.getRiskWarningCountMale());
        dashboardVo.setRiskWarningCountFemale(stats.getRiskWarningCountFemale());

        dashboardVo.setNormalCount(totalCount-stats.getResponseWarningCount()-stats.getAdaptiveWarningCount()-stats.getRiskWarningCount());
        dashboardVo.setNormalCountMale(totalCountMale-stats.getResponseWarningCountMale()-stats.getAdaptiveWarningCountMale()-stats.getRiskWarningCountMale());
        dashboardVo.setNormalCountFemale(totalCountFemale-stats.getResponseWarningCountFemale()-stats.getAdaptiveWarningCountFemale()-stats.getRiskWarningCountFemale());

        return dashboardVo;
    }

    private Map<String, DashboardDepartmentWarningVo> parseDepartmentWarningMap(List<DashboardDepartmentWarningDto> dtoList){
        return dtoList.stream()
                .filter(dto -> dto.getDeptName() != null) //确保键不为 null
                .collect(Collectors.toMap(
                        DashboardDepartmentWarningDto::getDeptName,
                        this::parseDashboardDepartmentWarningDtoToVo
                ));
    }

    private DashboardDepartmentWarningVo parseDashboardDepartmentWarningDtoToVo(DashboardDepartmentWarningDto dto) {
        // 查询预警指标
        DashboardDepartmentWarningVo vo = new DashboardDepartmentWarningVo();
        vo.setAdaptiveWarningCount(dto.getAdaptiveWarningCount());
        vo.setResponseWarningCount(dto.getResponseWarningCount());
        vo.setRiskWarningCount(dto.getRiskWarningCount());
        return vo;
    }

    private List<WarningPersonVo> parseWarningPersonDtoListToVo(List<WarningPersonDto> records) {
        return records.stream().map(this::parseWarningPersonDtoToVo).collect(Collectors.toList());
    }

    private WarningPersonVo parseWarningPersonDtoToVo(WarningPersonDto dto) {
        // 查询预警指标
        WarningPersonVo vo = new WarningPersonVo();
        List<WarningPropertiesVo> warningProperties = reportPropertiesMapper.selectWarningPropertiesVoByReportId(dto.getReportId());
        vo.setItems(warningProperties);
        vo.setUserId(dto.getUserId().toString());
        vo.setReportId(dto.getReportId().toString());
        vo.setName(dto.getName());
        vo.setSex(dto.getSex());
        vo.setAge(AgeUtils.getAge(dto.getBirthday()));
        vo.setCompanyName(dto.getCompanyName());
        vo.setDepartmentName(dto.getDepartmentName());
        vo.setPositionName(dto.getPositionName());
        vo.setWarningType(dto.getWarningType());
        return vo;
    }

}
